let requestUrl;

function addHandler() {
	requestUrl = "/chongwu_shizhuang_view_attribute/insert";

	$("#attribute_dialog").dialog("open");
}

function editHandler() {
	let data = $("#attribute_list").datagrid("getSelected");

	if(data) {
		requestUrl = "/chongwu_shizhuang_view_attribute/updateById";

		$("#id").val(data.id);
		$("#min").numberspinner("setValue", data.min);
		$("#max").numberspinner("setValue", data.max);
		$("#grade").numberspinner("setValue", data.grade);
		$("#propertyId").combobox("setValue", data.propertyId);

		$("#attribute_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

function deleteHandler(id) {
	$.messager.confirm("系统提示", "是否确认删除？", function(bool) {
		if (bool) {
			ajaxPost("/chongwu_shizhuang_view_attribute/deleteById", {
				id: id
			}, function(resp) {
				showMsg(resp.message);

				$("#attribute_list").datagrid("reload");
			}, error);
		}
	});
}

$(document).ready(function() {

	$("#grade").numberspinner({
		min: 0,
		max: 115,
		width: 150,
		required: true
	});

	fetchSelect("#propertyId", "/property/selectAll", {
		width: 150,
		required: true
	});

	$("#min").numberspinner({
		min: 1,
		width: 150,
		required: true
	});

	$("#max").numberspinner({
		min: 1,
		width: 150,
		required: true
	});

	$("#attribute_dialog").dialog({
		title: "预览属性",
		width: 500,
		height: 300,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#attribute_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#attribute_dialog").dialog("close");
						$("#attribute_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#attribute_form").form("clear");
				$("#attribute_dialog").dialog("close");
			}
		}]
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr * 15;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	let pageList = [15, 50, 100, 200, 500];

	$("#attribute_list").datagrid({
		url: "/chongwu_shizhuang_view_attribute/selectByPage",
		method: "get",
		height: height_table,
		fitColumns: true,
		rownumbers: true,
		pagination: true,
		pageList: pageList,
		pageSize: pageList[0],
		remoteFilter: true,
		clientPaging: false,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}],
		columns: [[
			{field: "id", hidden: true},
			{field: "grade", title: "装备等级", align: "center", width: 50,
				formatter: function(value) {
					return "<div>" + value + "级</div>";
				}
			},
			{field: "propertyId", title: "属性类型", align: "center", width: 50
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/property/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#property_" + rowIndex).html(data.name);
					}, error);

					return "<div class='blue' id='property_" + rowIndex + "' ></div>";
				}
			},
			{field: "max", title: "属性上限", align: "center", width: 50},
			{field: "min", title: "属性下限", align: "center", width: 50},
			deleteColumn
		]]
	});

});